package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entry.PageResult;
import com.itheima.entry.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jshstart
 * @creat 2020-11-11 19:29
 */
@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService  {
    @Reference
    private CheckGroupItemService checkGroupItemService;
    @Transactional
    @Override
    public void  addGroup(CheckGroupDto checkGroupDto) {
        this.saveOrUpdate(checkGroupDto.getCheckGroup());
        Integer groupId = checkGroupDto.getCheckGroup().getId();
        Integer[] checkitemIds = checkGroupDto.getCheckitemIds();
            checkGroupItemService.deleteById(groupId);
        if (groupId!=null&&checkitemIds.length!=0){
            for (Integer checkitemId : checkitemIds) {
               // baseMapper.addGroupItem(groupId,checkitemId);
                CheckGroupItem checkGroupItem = new CheckGroupItem();
                checkGroupItem.setGroupId(groupId);
                checkGroupItem.setCheckitemId(checkitemId);
                checkGroupItemService.save(checkGroupItem);
            }
        }
    }

    @Override
    public PageResult findAllGroupByPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> page=null;
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString()!=null&&"".equals(queryPageBean.getQueryString())==false){
            queryWrapper.and(wrapper ->{
                wrapper.like("name",queryPageBean.getQueryString()).or()
                .like("code",queryPageBean.getQueryString()).or()
                .like("help_code",queryPageBean.getQueryString());
            });
        }
        page =page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        List<CheckGroup> list = list(queryWrapper);
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        CheckGroup checkGroup = baseMapper.selectById(id);
        if (checkGroup!=null){
            checkGroup.setIs_delete(1);
            this.updateById(checkGroup);
        }
    }

    @Override
    public List<CheckGroupVo> findAllGroupBySetmealId(Long id) {
       List<CheckGroupVo> list=baseMapper.findAllGroupBySetmealId(id);
        return list;
    }
}
